import StroTo from './StrTo.js'

/**
 * 名词
 * 感觉这个才是系统的核心
 */
export default class Noun{



    constructor(){
        this.sign={a:" ",b:"\n",c:"\n\n"};
        this.tab={A:'a',
            B:'b',C:'c'}
    }

    /**
     * 四比特的编码，合并成八比特编码，然后通过utf-8转成字符串
     * @param data
     * @returns {string}
     */
      backTo(data){
        let _this=this;
        let res=""
        let buffData=  new Uint8Array(data);
        let numtell="";

        for(let i=0;i<buffData.length;i++){
            let num=buffData[i].toString(2);
            let buffNum =  (Array(8).join(0) + num).slice(-8)
            let code0= parseInt(buffNum.substr(0,4),2).toString()
            let code1= parseInt(buffNum.substr(4,7),2).toString()

            if(code0 >=10 && code0<=15){
                let str=StroTo.numToUtf8(numtell);
                let le=this.sign[parseInt(code0,10).toString(16)];
                res+=str+le;
                numtell="";
            }else {
                numtell+=code0;
            }

            if(code1 >=10 && code1<=15){
                let str=StroTo.numToUtf8(numtell);
                let le=this.sign[parseInt(code1,10).toString(16)];
                res+=str+le;
                numtell="";
            }else {
                numtell+=code1;
            }

        }
        let str=StroTo.numToUtf8(numtell);
        res+=str;

        return res;


    }


    /**
     * 字符串 utf-8 转成 四比特编码 buff
     * @param txtData
     * @returns {*}
     */
    justTo(txtData){
        let _this=this;
        let reg =new RegExp(/\n{2,}/);
        let rexList=txtData.split(reg)
        let res="";
        for (let n=0;n<rexList.length;n++){
            let thatList=rexList[n].split("\n");

            for (let i=0;i<thatList.length;i++){
                let wordList=thatList[i].split(/\s+/)
                for(let l=0;l<wordList.length;l++){
                    if(l >= wordList.length-1){
                        let num=StroTo.utf8ToNum(wordList[l]);
                        res+=num+_this.tab.B;
                    }else {
                        let num=StroTo.utf8ToNum(wordList[l]);
                        res+=num+_this.tab.A;
                    }
                }
                if(i >= thatList.length-1){
                    res+= _this.tab.C;
                }

            }
        }

        res=res.slice(0,res.length-1);
        let arr=StroTo.numToCode(res);
        let resBuff=StroTo.toBuff(arr);

        return resBuff;
    }


    /**
     * 字符串 utf-8 转成 四比特编码 buff
     * @param txtData
     * @returns {*}
     */
    utfToFour(txtData){
        let _this=this;
        let reg =new RegExp(/\n{2,}/);
        let rexList=txtData.split(reg)
        let res="";
        for (let n=0;n<rexList.length;n++){
            let thatList=rexList[n].split("\n");

            for (let i=0;i<thatList.length;i++){
                let wordList=thatList[i].split(/\s+/)
                for(let l=0;l<wordList.length;l++){
                    if(l >= wordList.length-1){
                        let num=StroTo.utf8ToNum(wordList[l]);
                        res+=num+_this.tab.B;
                    }else {
                        let num=StroTo.utf8ToNum(wordList[l]);
                        res+=num+_this.tab.A;
                    }
                }
                if(i >= thatList.length-1){
                    res+= _this.tab.C;
                }

            }
        }

        res=res.slice(0,res.length-1);
        return res;
    }
}